package com.cloud.controller;

import com.cloud.domian.Order;
import com.cloud.domian.Stock;
import com.cloud.service.IOrderService;
import com.cloud.service.IStockService;
import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Author wenbo
 * @Date 2022/12/30 11:30
 **/
@RestController
@RequestMapping("/stock")
public class StockController {


    @Resource
    private IStockService iStockService;

    @Resource
    private IOrderService iOrderService;


    @GetMapping("/decStock")
    @GlobalTransactional(rollbackFor = Exception.class)
    public String decStock(
            @RequestParam Long productId,
            @RequestParam Long productSpecs){
        Order order = new Order();
        order.setOrderNumber("HX9786786");
        iOrderService.save(order);
        Stock stock = iStockService.getById(12L);
        stock.setProductNum(stock.getProductNum() - 1);
        iStockService.updateById(stock);
        int a = 1/0;
        System.out.println();

        return "成功";
    }


    @GetMapping("/decStock2")
    public String decStock2(){
        String xid = RootContext.getXID();
        System.err.println(xid);
        Stock stock = iStockService.getById(12L);
        stock.setProductNum(stock.getProductNum() - 1);
        iStockService.updateById(stock);
        //int a = 1/0;
        System.out.println();
        return "成功";
    }
}
